<template>
  <el-card style="width: 500px;">
    <el-form label-width="80px" size="small">
      <el-upload
          class="avatar-uploader"
          :action="'http://localhost:9090/file/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <img :src="imgSrc" v-show="false"/>
      <el-form-item label="用户名">
        <el-input v-model="form.username" disabled autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nickname" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  
</template>

<script>
// import {serverIp} from "../../public/config";
import Bus from '../Bus'
export default {
  name: "Person",
  data() {
    return {
      // serverIp: serverIp,
      updateMessage:'',
      imgSrc:'',
      form: {},
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      // console.log(res)
      // console.log('res:',res)
      this.form = res
      // console.log('form:',this.form);
    })
  },
  methods: {
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.username)).data
    },
    async save() {
      let res=await this.request.post("/user", this.form)
      if (res.code === '200') {
          this.$message.success("保存成功")
          // console.log('更新成功:',res);
          // 触发父级更新User的方法
          // this.$emit("refreshUser")
          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            console.log('重新获取数据库用户信息:',res);
            res.token = JSON.parse(sessionStorage.getItem("user")).token
            sessionStorage.setItem("user", JSON.stringify(res))
            this.user=JSON.parse(sessionStorage.getItem('user'))
          })
          this.updateMessage='我更新了头像'
          Bus.$emit('更新头像',this.updateMessage)
        } else {
          this.$message.error("保存失败")
        }
    },
    handleAvatarSuccess(res) {
      this.imgSrc=res
      this.form.avatarUrl = res
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
